package com.zhbr.mvp.ui.fragment;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zhbr.api.IApiService;
import com.zhbr.common.RetrofitHelper;
import com.zhbr.common.rx.BaseSubscriber;
import com.zhbr.common.rx.RxUtil;
import com.zhbr.entity.MPageInfo;
import com.zhbr.entity.TGdfwPqgd;
import com.zhbr.mvp.base.AppConstants;

import java.util.ArrayList;
import java.util.List;

import retrofit2.http.Query;
import rx.Subscription;

import static com.zhbr.common.rx.RxUtil.applySchedulers;

public class PageViewModel1 extends ViewModel {

    private MutableLiveData<List<TGdfwPqgd>> list;

    public PageViewModel1(){
        list.setValue(new ArrayList<>());
        refreshData(1,5);
    }

    public MutableLiveData<List<TGdfwPqgd>>  getList() {
        return list;
    }

    public void refreshData(int pageNum,int pageSize){
        IApiService mApi =RetrofitHelper.getInstance().createApiService(AppConstants.BASE_SERVER_IP);
        mApi.getAllOrder(pageNum,pageSize).compose(applySchedulers()).compose(RxUtil.handleResult()).subscribe(new BaseSubscriber<MPageInfo<List<TGdfwPqgd>>>() {
            @Override
            public void onSuccess(MPageInfo<List<TGdfwPqgd>> data) {
                Log.d("PageViewModel1", "onSuccess: 获取数据成功"+data.getSize());
                list.setValue(data.getList());
            }

            @Override
            public void onFailed(Throwable ex, String code, String msg) {

            }

            @Override
            public void onError() {

            }
        });
    }
}
